/*******************************************************************************
 * Copyright (c) 2011 Technische Universität Darmstadt
 * 					  Fachbereich Informatik
 * 					  Theoretische Informatik - Kryptographie und Computeralgebra
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/

var info = Components.classes["@mozilla.org/xre/app-info;1"]
.getService(Components.interfaces.nsIXULAppInfo);

var columnHandler = {
		getCellText: function(row, col) {
			var uri = gDBView.getURIForViewIndex(row);
			var hdr = gDBView.db.GetMsgHdrForKey(gDBView.getKeyAt(row));
			var subject = hdr.mime2DecodedSubject;
			if (subject.indexOf("-----SUBJECT ENCRYPTED-----") > -1){
				var win = Components.classes["@mozilla.org/appshell/window-mediator;1"].
				getService(Components.interfaces.nsIWindowMediator).
				getMostRecentWindow("mail:3pane");
				if(win.TheChasingDragon == null || win.TheChasingDragon.cryptoBirdJava == null) return "";
				var dbSubject = win.TheChasingDragon.cryptoBirdJava.decryptSubject(uri); //read subject from database
				if(dbSubject == "---ERROR---" || dbSubject == ""){
					return "Decryption Error"; //TODO: andere Bezeichnung, ggf lokalisiert?
				} else {
					return dbSubject;
				}
			} else{
				return subject;
			}
		},
		getSortStringForRow: function(hdr) {
			var subject = hdr.getStringProperty("subject");
			var uri = hdr.folder.getUriForMsg(hdr);
			if (subject.indexOf("-----SUBJECT ENCRYPTED-----") > -1){
				var win = Components.classes["@mozilla.org/appshell/window-mediator;1"].
				getService(Components.interfaces.nsIWindowMediator).
				getMostRecentWindow("mail:3pane");
				var dbSubject = win.TheChasingDragon.cryptoBirdJava.decryptSubject(uri);
				if(dbSubject == "---ERROR---" || dbSubject == ""){
					return "Decryption Error";
				} else {
					return dbSubject;
				}
			} else{
				return subject;
			}
		},
		isString:            function() {return true;},
		getCellProperties:   function(row, col, props){},
		getRowProperties:    function(row, props){},
		getImageSrc:         function(row, col) {return null;},
		getSortLongForRow:   function(hdr) {return 0;}
}

window.addEventListener("load", doOnceLoaded, false);

function doOnceLoaded() {
	var ObserverService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
	ObserverService.addObserver(CreateDbObserver, "MsgCreateDBView", false);
}

var CreateDbObserver = {
		observe: function(aMsgFolder, aTopic, aData)
		{  
			addCustomColumnHandler();
		}
}

function addCustomColumnHandler() {
	var decrypt = true;
	if(info.version.charAt(0)<5){
		decrypt = navigator.preference('cryptoBird.decrypt_subject'); 
	}else{	
		try{
			decrypt = Services.prefs.getBoolPref('cryptoBird.decrypt_subject');
		}catch(e){
		}
	}
	if(decrypt){ //check if decryption is activated in properties
		gDBView.addColumnHandler("subjectCol", columnHandler);
	}
}


